package com.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 用于文件操作操作的工具类
 * 
 * @author dan
 * 
 */
public class FileUtil {

    private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 获取文件中的内容, 以long显示, 失败则返回0L
     * 
     * @param filename
     * @return
     */
    public static long getLong(String filename) {
        long id = 0L;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(filename));
            String s = in.readLine();
            if (s != null) {
                try {
                    id = Long.parseLong(s);
                } catch (NumberFormatException e) {
                    LOG.error("File does not contain a parsable long: " + e);
                }
            }
        } catch (IOException e) {
            LOG.error("Read file error[" + filename + "]" + e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return id;
    }

    /**
     * 写入long形的内容到文件中
     * 
     * @param filename
     * @param id
     * @throws IOException
     */
    public static void setLong(String filename, long id)
            throws IOException {
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                filename)));
        out.print(id);
        out.close();
    }

    /**
     * 从文件中读取一个long形的列表
     * 
     * @param filename
     * @return
     */
    public static List<Long> getLongList(String filename) {
        List<Long> list = new ArrayList<Long>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            while (true) {
                String s = in.readLine();
                if (s != null) {
                    list.add(Long.parseLong(s));
                } else {
                    break;
                }
            }
            in.close();
        } catch (NumberFormatException e) {
            LOG.error("String does not contain a parsable long: " + e);
        } catch (IOException e) {
            LOG.error("Read file error[" + filename + "]" + e);
        }
        return list;
    }

    /**
     * 在文件中写入long行的列表
     * 
     * @param filename
     * @param list
     * @throws IOException
     */
    public static void setLongList(String filename, long[] list)
            throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
        for (long l : list) {
            bw.write(l + "\n");
        }
        bw.close();
    }

    /**
     * 在文件中写入String行的列表
     * 
     * @param filename
     * @param list
     * @throws IOException
     */
    public static void setStringList(String filename, String[] list)
            throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
        }

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));
        for (String l : list) {
            bw.write(l + "\n");
        }
        bw.close();
    }

    /**
     * 从文件中读取一个int形的列表
     * 
     * @param filename
     * @return
     */
    public static List<Integer> getIntList(String filename) {
        List<Integer> list = new ArrayList<Integer>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            while (true) {
                String s = in.readLine();
                if (s != null) {
                    list.add(Integer.parseInt(s));
                } else {
                    break;
                }
            }
            in.close();
        } catch (NumberFormatException e) {
            LOG.error("String does not contain a parsable int: " + e);
        } catch (IOException e) {
            LOG.error("Read file error[" + filename + "]" + e);
        }
        return list;
    }

    /**
     * 从文件中读取一个String形的列表
     * 
     * @param filename
     * @return
     */
    public static List<String> getStringList(String filename) {
        List<String> list = new ArrayList<String>();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "utf-8"));
            while (true) {
                String s = in.readLine();
                if (s != null) {
                    list.add(s);
                } else {
                    break;
                }
            }
            in.close();
        } catch (IOException e) {
            LOG.error("Read file error[" + filename + "]" + e);
        }
        return list;
    }
    public static String getFirstLine(String filename) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "utf-8"));
            String s = in.readLine();
            in.close();
            return s;
        } catch (IOException e) {
            LOG.error("Read file error[" + filename + "]" + e);
        }
        return null;
    }
}
